import json
from openpyxl import Workbook


def write(row, column, value):
    """
    写到指定单元格中
    :param row: 行
    :param column: 列
    :param value: 值
    :return:
    """
    row += 1
    if row == 1:
        row = 2
    else:
        row += 1
    ws.cell(row, column, value)


if __name__ == '__main__':
    # 创建excel并操作
    wb = Workbook()
    ws = wb.active
    header = ['编号', '电影名', '电影别名', '电影封面', '电影分类', '上映日期', '电影时长', '评分', '国家']
    englishHeader = ['id', 'name', 'alias', 'cover', 'categories', 'published_at', 'minute', 'score', 'regions']
    for k, v in enumerate(header):
        ws.cell(1, k + 1, v)
    # 读取文件
    with open('../spa1.jl', 'r', encoding='utf-8') as fileHandle:
        while True:
            line = fileHandle.readline()
            # 如果为空跳出循环
            if not line:
                break
                # 一行行遍历读取文本中的数据
            for dataKey, dataValue in enumerate(json.loads(line)['data']):
                print("****" + str(dataKey))
                id = dataValue['id']
                if id == 0:
                    id += 2
                else:
                    id += 1
                for k, v in enumerate(englishHeader):
                    k += 1
                    ws.cell(id, k, str(dataValue[v]))
                    print(dataValue['id'], 1, dataValue['id'])
    # 写出文件
    wb.save('spa1Data.xlsx')
